import { VoucherJoinLs, VoucherJoinLsKey } from "qqlx-core";

import { doBitsToDecimal, doDecimalToBits } from "@lib/com.binary";

/** 将凭证的查询对象，转换成二进制字符串 */
export function getVoucherJoinDecimal(option: Partial<Record<VoucherJoinLsKey, true>>) {
  const value = doDecimalToBits(0, VoucherJoinLs.length);
  const bits = value.bits.split("");

  for (const key in option) {
    const index = VoucherJoinLs.findIndex((k) => k === key);
    if (index > -1) bits[index] = "1";
  }
  return doBitsToDecimal(bits.join("")).decimal;
}

/** 将二进制字符串，转换成凭证的查询对象 */
export function getVoucherJoinRecord(decimal: number) {
  const value = doDecimalToBits(decimal, VoucherJoinLs.length);
  const bits = value.bits.split("");

  const record: Partial<Record<VoucherJoinLsKey, true>> = {};
  for (const index in bits) {
    const key = VoucherJoinLs[index];
    if (bits[index] === "1") record[key] = true;
  }
  return record;
}
